/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package communication;

import flexjson.JSONDeserializer;
import flexjson.JSONException;
import flexjson.JSONSerializer;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import model.Animal;

/**
 *
 * @author Fernando
 */
public class QAnimal extends RequestServer {

    private String table_name = "animal";
    private String json = "";
    private JSONArray array;
    private ArrayList<JSONObject> objList = new ArrayList<JSONObject>();
    private Integer idAnimal = null;
    private Integer idVisual;
    private Integer horn;
    private String comment;
    private Integer sex;

    public static void main(String[] args) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {
        //USAGE
        ///*GET ALL REGISTERS
        QAnimal SV = new QAnimal();
        
        ArrayList<Animal> All = SV.GetAll();
        
        for (Animal V : All){
        
        System.out.println(V.getComment());        
        }
        //*/
        /*GET ONE REGISTER
        QEvent SV = new QEvent(4);
        //OR: QVaccine SV = new QVaccine(name); 
        
        Event V = SV.GetObject(0); // index 0, the first returned value
        System.out.println(V.getAnimal_idAnimal());
        //*/
        /*DELETE
        QAnimal QV = new QAnimal();
        
        String R = QV.Delete(null, null, null, null, null);
        //*/
        /*UPDATE
        QAnimal QV = new QAnimal();
        
        String R = QV.Update(2, 9128371, 1,"aAAAAA", 0);
        //*/
        /*INSERT 
        QAnimal QV = new QAnimal();
        
        String answer = QV.Insert(12, 1,"comentario", 1);
        //*/
    }

    public JSONObject Serialize(Animal V) throws JSONException, org.codehaus.jettison.json.JSONException {

        JSONSerializer serializer = new JSONSerializer();

        this.json = serializer.serialize(V);

        JSONObject obj = new JSONObject(this.json);

        obj.remove("class");
        return obj;
    }

    public String Insert(Integer idVisual, Integer horn, String comment, Integer sex) throws UnsupportedEncodingException, IOException, JSONException, org.codehaus.jettison.json.JSONException {

        Animal V = new Animal(idVisual, horn, comment, sex);

        JSONObject obj = this.Serialize(V);

        obj.remove("idAnimal");

        String post = this.post("[" + obj.toString() + "]", this.table_name);

        return post;
    }

    public String Update(int id, Integer idVisual, Integer horn, String comment, Integer sex) throws UnsupportedEncodingException, IOException, JSONException, org.codehaus.jettison.json.JSONException {

        Animal V = new Animal(idVisual, horn, comment, sex);
        this.idAnimal = id;

        JSONObject obj = this.Serialize(V);

        obj.remove("idAnimal");

        this.json = obj.toString();

        String parameters = PrepareParameters();

        System.out.println(this.json);
        System.out.println(parameters);

        String put = this.put(this.table_name, this.json, parameters);

        return put;
    }

    public String Delete(Integer id, Integer idVisual, Integer horn, String comment, Integer sex) throws IOException {

        this.idAnimal = id;
        this.idVisual = idVisual;
        this.horn = horn;
        this.comment = comment;
        this.sex = sex;

        String delete = this.delete(this.table_name, PrepareParameters());

        return delete;
    }

    private boolean SetJsonList() throws JSONException, org.codehaus.jettison.json.JSONException {

        if (this.json == "") {
            return false;
        }

        this.array = new JSONArray(this.json);

        for (int i = 0; i < array.length(); i++) {

            JSONObject tmp = array.getJSONObject(i);
            objList.add(tmp);
        }
        return true;
    }

    public Animal GetObject(int index) throws JSONException, org.codehaus.jettison.json.JSONException {

        if ("".equals(this.json)) {
            Animal A = new Animal();
            return A;
        }
        this.array = new JSONArray(this.json);

        JSONObject obj = null;

        obj = objList.get(index);

        obj.put("class", Animal.class.getCanonicalName());

        String djson = obj.toString();

        Animal V = new JSONDeserializer<Animal>().deserialize(djson);

        return V;
    }

    public ArrayList<Animal> GetAll() throws JSONException, IOException, org.codehaus.jettison.json.JSONException {

        this.json = this.get(this.table_name, null);
        SetJsonList();

        ArrayList<Animal> Varray = new ArrayList<Animal>();

        for (int i = 0; i < objList.size(); i++) {

            Varray.add(GetObject(i));
        }
        return Varray;
    }

    public String PrepareParameters() {

        String sex = "";
        String comment = "";
        String horn = "";
        String visual = "";
        String id = "";
        String ret = "";

        id = (this.idAnimal != null) ? ("idAnimal="
                + this.idAnimal.toString()) : "";
        
        horn = (this.horn != null) ? ("horn="
                + this.horn.toString()) : "";

        visual = (this.idVisual != null) ? ("idVisual="
                + this.idVisual.toString()) : "";

        comment = (this.comment != null) ? ("comment="
                + this.comment.toString()) : "";
        
        sex = (this.sex != null) ? ("sex="
                + this.sex.toString()) : "";
        
        if (!"".equals(id) || !"".equals(horn) || !"".equals(visual) || !"".equals(sex) || !"".equals(comment)) {
            return "?" + id + "&" + horn + "&" + visual +"&"+sex +"&"+comment;
        }
        return ret;
    }

    public QAnimal(Integer idAnimal) throws IOException, JSONException, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONException, org.codehaus.jettison.json.JSONException {

        super();

        if (idAnimal != null) {

            this.idAnimal = idAnimal;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QAnimal(Integer idAnimal, Integer idVisual) throws IOException, JSONException, org.codehaus.jettison.json.JSONException {
        super();

        if (idVisual != null) {
            this.idVisual = idVisual;
            this.json = this.get(this.table_name, this.PrepareParameters());
        } else {
            this.json = this.get(this.table_name, null);
        }
        SetJsonList();
    }

    public QAnimal() throws IOException, JSONException {
        super();
    }

    public JSONArray getArray() {
        return array;
    }

    public void setArray(JSONArray array) {
        this.array = array;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public Integer getHorn() {
        return horn;
    }

    public void setHorn(Integer horn) {
        this.horn = horn;
    }

    public Integer getIdAnimal() {
        return idAnimal;
    }

    public void setIdAnimal(Integer idAnimal) {
        this.idAnimal = idAnimal;
    }

    public Integer getIdVisual() {
        return idVisual;
    }

    public void setIdVisual(Integer idVisual) {
        this.idVisual = idVisual;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

    public ArrayList<JSONObject> getObjList() {
        return objList;
    }

    public void setObjList(ArrayList<JSONObject> objList) {
        this.objList = objList;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getTable_name() {
        return table_name;
    }

    public void setTable_name(String table_name) {
        this.table_name = table_name;
    }
    
}
